package com.zyzd.retrofit.base;

import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzy on 2018/5/3 0003.
 * description ${列表Adapter 基类}
 */

public abstract class BaseAdapter<VH extends RecyclerView.ViewHolder, T> extends RecyclerView.Adapter<VH> {
    protected final Context mContext;
    protected final LayoutInflater mInflater;
    protected ArrayList<T> mData = new ArrayList<>();
    protected OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public BaseAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void addAll(boolean isClear, List<T> list) {
        if (list == null) new ArrayList<>();
        int oldSize = mData.size();
        if (isClear) mData.clear();
        mData.addAll(list);
        int newSize = mData.size();
        if (oldSize > newSize) {
            notifyItemRangeRemoved(getHeaderLayoutCount() + newSize, oldSize - newSize);
        }
        notifyItemRangeChanged(getHeaderLayoutCount(),mData.size());
    }

    public void add(T data) {
        if (data == null) return;
        mData.add(data);
        notifyItemInserted(mData.size() + getHeaderLayoutCount());

    }

    public void add(@IntRange(from = 0) int position, T data) {
        if (data == null) return;
        mData.add(position, data);
        notifyItemInserted(position + getHeaderLayoutCount());
    }

    public void remove(@IntRange(from = 0) int position) {
        mData.remove(position);
        int internalPosition = position + getHeaderLayoutCount();
        notifyItemRemoved(internalPosition);
        notifyItemRangeChanged(internalPosition, mData.size() - internalPosition);
    }

    public T getItem(int position) {
        return mData.get(position - getHeaderLayoutCount());
    }

    @Override
    public int getItemCount() {
        return mData.size() + getHeaderLayoutCount() + getFooterLayoutCount();
    }

    public List<T> getItems() {
        return mData;
    }

    /**
     * @return 普通条目（列表数据）之上的条目数量，用于矫正
     */
    protected int getHeaderLayoutCount() {
        return 0;
    }

    /**
     * @return 普通条目（列表数据）之下的条目数量，用于矫正
     */
    protected int getFooterLayoutCount() {
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListerner(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, final int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(position);
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return mOnItemLongClickListener.onItemLongClick(position);
                }
            });
        }
        onBindHolder(holder, position);
    }

    protected abstract void onBindHolder(@NonNull VH holder, int position);
}
